package com.stevenmoon.csdnplus.presenter;

import com.stevenmoon.csdnplus.bean.headline.ColumnHeadlineReqBean;
import com.stevenmoon.csdnplus.bean.headline.HeadlineListBean;
import com.stevenmoon.csdnplus.fragment.HeadlineColumnListFragment;
import com.stevenmoon.csdnplus.logic.HeadlineManager;
import com.stevenmoon.csdnplus.presenter.base.MvpBaseNoCheckPresenter;

import java.util.List;

import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by WangJun on 2016/4/12.
 */
public class HeadlineListPresenter extends MvpBaseNoCheckPresenter<HeadlineColumnListFragment> {

    private int mPageIndex = 1;

    private int mComid;

    public HeadlineListPresenter(int comid){
        mComid = comid;
    }

    public void loadNewData(){
        getView().showLoading();
        ColumnHeadlineReqBean reqBean = new ColumnHeadlineReqBean();
        reqBean.setPage(1);
        reqBean.setComid(mComid);
        Subscription s = HeadlineManager.getColumnHeadlineList(reqBean)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<List<HeadlineListBean>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        getView().hideLoading();
                        getView().loadNewDataFail(e.getMessage());
                    }

                    @Override
                    public void onNext(List<HeadlineListBean> headlineInfoBeen) {
                        getView().hideLoading();
                        //TODO 将新数据缓存到本地数据库
                        getView().loadNewDataSuccess(headlineInfoBeen);
                    }
                });

        getCompositeSubscription().add(s);
        
    }

    public void loadMoreData(){
        ColumnHeadlineReqBean reqBean = new ColumnHeadlineReqBean();
        reqBean.setPage(mPageIndex+1);
        reqBean.setComid(mComid);
        Subscription s = HeadlineManager.getColumnHeadlineList(reqBean)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<List<HeadlineListBean>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        getView().loadMoreDataFail(e.getMessage());
                    }

                    @Override
                    public void onNext(List<HeadlineListBean> headlineInfoBeen) {
                        if(headlineInfoBeen == null || headlineInfoBeen.isEmpty()){
                            getView().showNoMoreData();
                        }else{
                            getView().loadMoreDataSuccess(headlineInfoBeen);
                            mPageIndex++;
                        }

                    }
                });

        getCompositeSubscription().add(s);
    }


}
